<?php
FLEA::loadClass('Controller_Base');
class Controller_Products extends Controller_Base {
	var $_modelProducts;
	var $_modelProductClasses;
	function Controller_Products() {
		$this->_modelProducts = & FLEA::getSingleton("Model_Products");
		$this->_modelProductClasses = & FLEA::getSingleton("Model_ProductClasses");
		$this->_modelProductWords = & FLEA::getSingleton("Model_ProductWords");
		$this->_modelTags = & FLEA::getSingleton("Model_Tags");
	}
	function actionIndex() {
		$pcid = (int)$_GET['pcid'];
		$page = isset($_GET['page']) ? (int)$_GET['page'] : 0;
		FLEA::loadClass('FLEA_Helper_Pager');
		$table =& $this->_modelProducts->getTable();
		$pager =& new FLEA_Helper_Pager($table, $page,20,"pcid=$pcid");
		$pk = $table->primaryKey;
		$rowset = $pager->findAll();
		$this->_setBack();
//		include(TPL_DIR . '/products.php');
	}
	function actionShow() {
		$pid = (int) $_POST["pid"];
		$product = $this->_modelProducts->getProduct($pid);
		$class = $this->_modelProductClasses->getProductClass($product["pcid"]);
		$product_words = $this->_modelProductWords->getProductWords($pid);
		include(TPL_DIR."/product.php");
		include(TPL_DIR."/product_word_add.php");
	}
	function actionAddnew() {
		$pcid=(int)$_POST["pcid"];
		$class = $this->_modelProductClasses->getProductClass($pcid);
		$subclasses = $this->_modelProductClasses->getSubClass($pcid);
		include("./Plugin/fckeditor/fckeditor.php");
		$oFCKeditor = new FCKeditor('FCKeditor1');
		$oFCKeditor->BasePath = './Plugin/fckeditor/';
		$oFCKeditor->Value = 'Default text in editor';
		include(TPL_DIR . '/product_add.php');
	}
	function actionAddProduct() {
	/**
	 * 传送过来的数据有	可能的子分类,产品名称,产品描述,资源文件,tags,uid
	 */
//预先假定一个uid
		$uid = 1;
//tags分解原则:以空格分隔,所以tag中有空格的最好以_或者-代替
		$pcid = $this->_modelProductClasses->getpcid($_POST['selclass'],$_POST['classid']);
	//把tags记录进去,存储产品
		$new_product = array(
			'pcid'=>$pcid,
			'product_name'=>$_POST['product_name'],
			'product_desc'=>$_POST['product_desc'],
			'uid'=>$uid
		);
		$pid = $this->_modelProducts->addProduct($new_product);
		$this->_modelTags->addtags($_POST['tags'],$pid,$uid);//产品是否需要记录tag???
	}
}
?>